package JPLXtandemGA;

import java.util.Formatter;
import java.util.Random;

public class DoubleParameter implements IParameter<DoubleParameter> {

	private final String name;
	private final double value;
	private final double lowerBound;
	private final double upperBound;
	private final double std;
	private final Random RNG = new Random();
	
	
	
	public DoubleParameter(String name, double value, double lowerBound,
			double upperBound, double std) {
		this.name = name;
		this.value = value;
		this.lowerBound = lowerBound;
		this.upperBound = upperBound;
		this.std = std;
	}

	@Override
	public DoubleParameter mutate() {
		double newValue = constrain( value + RNG.nextGaussian()*std );
		return new DoubleParameter(name, newValue, lowerBound, upperBound, std);
	}

	private double constrain(double d) {
		if( d < lowerBound ) return lowerBound;
		if( d > upperBound ) return upperBound;
		return d;
	}

	@Override
	public String name() {
		return name;
	}

	@Override
	public DoubleParameter randomize() {
		double interval = upperBound - lowerBound;
		double newValue = RNG.nextDouble()*interval + lowerBound;
		return new DoubleParameter(name, newValue, lowerBound, upperBound, std);
	}

	@Override
	public String xmlRepresentation() {
		Formatter fmt = new Formatter();
	    fmt.format("%.4f", value);
		return Parameters.makeNote(name, fmt.toString());
	}

	@Override
	public String valueToString() {
		Formatter fmt = new Formatter();
	    fmt.format("%.4f", value);
		return  fmt.toString();
		
	}

}
